import modelExtend from 'dva-model-extend'
import { pathMatchRegexp } from 'utils'
import { <#list modalProps.fields as field><#if (field.type=='TreeSelect')>get${field.name?cap_first}Tree,</#if></#list>query${api?cap_first},query${api?cap_first}Detail,create${api?cap_first},update${api?cap_first},remove${api?cap_first} } from 'api'
import { pageModel } from 'utils/model'
import { message } from 'antd'

export default modelExtend(pageModel, {
	namespace: '${namespace}',

	state: {
		modalVisible:false,
		currentItem: {},
		itemDetails:[],<#if (modalProps.pagination)>
		itemDetailsPagination:{},</#if>
		filterItems:{},
		<#list modalProps.fields as field><#if (field.type=='TreeSelect')>${field.name}Tree:[],</#if></#list>
	},

	subscriptions: {
		setup({ dispatch, history }) {
			history.listen(location => {
				if (pathMatchRegexp('${url}', location.pathname)) {
					const payload = location.query || { page: 1, pageSize: 10 }
					dispatch({
						type: 'query',
						payload,
					})
					<#list modalProps.fields as field><#if (field.type=='TreeSelect')>
					dispatch({
						type: 'get${field.name?cap_first}Tree',
						payload,
					})
					</#if></#list>
				}
			})
		},
	},

	effects: {
		*query({ payload = {} }, { call, put }) {
			const data = yield call(query${api?cap_first}, payload)
			const {error,result}=data
			if (!error) {
				yield put({
					type: 'querySuccess',
					payload: {
						list: result.rows,
						pagination: {
							current: Number(payload.page) || 1,
							pageSize: Number(payload.pageSize) || 10,
							total: result.total,
						},
					},
				})
			}
		},
		
		*queryDetail({ payload = {} }, { call, put }) {
			const data = yield call(query${api?cap_first}Detail, payload)
			const {error,result}=data
			if (!error) {
			<#if (modalProps.pagination)>
				yield put({type:'updateState',payload:{itemDetails:result.rows,itemDetailsPagination:{current: Number(payload.page) || 1,pageSize: Number(payload.pageSize) || 10,total: result.total}}})
			<#else>
				yield put({type:'updateState',payload:{itemDetails:result}})
			</#if>
			}
		},
		
		<#list modalProps.fields as field><#if (field.type=='TreeSelect')>
		*get${field.name?cap_first}Tree({ payload = {} }, { call, put }) {
			const data = yield call(get${field.name?cap_first}Tree, payload)
			const {error,result}=data
			if (!error) {
				yield put({
					type: 'updateState',
					payload: {
						${field.name}Combo:result
					},
				})
			}
		},
		</#if></#list>

		<#if (remove)>
		*delete({ payload }, { call, put, select }) {
			const data = yield call(remove${api?cap_first}, payload)
			if (!data.error) {
				message.success('操作成功')
			} else {
				throw data
			}
		},
		</#if>

		<#if (add)>
		*create({ payload }, { call, put }) {
			const data = yield call(create${api?cap_first}, payload)
			if (!data.error) {
				message.success('操作成功')
				yield put({ type: 'hideModal' })
			} else {
				throw data
			}
		},
		</#if>

		<#if (update)>
		*update({ payload }, { select, call, put }) {
			const id = yield select(({ ${namespace} }) => ${namespace}.currentItem.id)
			const obj = { ...payload, id }
			const data = yield call(update${api?cap_first}, obj)
			if (!data.error) {
				message.success('操作成功')
				yield put({ type: 'hideModal' })
			} else {
				throw data
			}
		},
		</#if>
		
		*updateItemDetails({ payload }, { select, call, put }) {
			const {rows}=payload
			const itemDetails = yield select(({ ${namespace} }) => ${namespace}.itemDetails)
			let arr=[];
			for(let i=0;i<rows.length;i++){
				const row=rows[i]
				let exist=false
				for(let j=0;j<itemDetails.length;j++){
					const detail=itemDetails[j]
					if(row.id==detail.fkId){
						exist=true
						break
					}
				}
				if(!exist){
					arr.push({partName:row.name,partCode:row.code,number:0,unit:row.unit,fkId:row.id})
				}
			}
			yield put({type:'updateState',payload:{itemDetails:arr.concat(itemDetails)}})
		}
	},

	reducers: {
		showModal(state, { payload }) {
			return { ...state, ...payload, modalVisible: true }
		},

		hideModal(state) {
			return { ...state, modalVisible: false }
		},
	},

})